package ticket.model;

import org.apache.commons.lang3.math.NumberUtils;
import ticket.enums.SeatType;

/**
 * 车票信息
 *
 * Created by forvoyager@outlook.com on 2018-10-08 14:36.
 */
public class TicketDto {

  private String secretHBStr;
  private String secretStr;

  private String train_no; // 车次编号
  private String station_train_code; // 车次
  private String start_station_telecode; // 起点站
  private String end_station_telecode; // 终点站

  private String from_station_name; // 出发站名称
  private String to_station_name; // 到达站名称
  private String from_station_no; // 出发站编号
  private String to_station_no; // 到达站编号
  private String from_station_telecode; // 出发站编码
  private String to_station_telecode; // 到达站编码

  private String start_train_date; // 发车日期 时间格式：yyyy-MM-dd
  private String start_time; // [出发站]出发时间 时间格式：HH:mm
  private String arrive_time; // [到达站]到达时间 时间格式：HH:mm
  private String lishi; // 历时/耗时 时间格式：HH:mm

  private String gg_num;
  private String gr_num; // 高级软卧余票
  private String qt_num;
  private String rw_num; // 软卧余票
  private String rz_num; // 软座余票
  private String tz_num; // 特等座余票
  private String wz_num; // 无座余票
  private String yb_num;
  private String yw_num; // 硬卧余票
  private String yz_num; // 硬座余票
  private String ze_num; // 二等座余票
  private String zy_num; // 一等座余票
  private String swz_num; // 商务座余票
  private String srrb_num; // 动卧余票

  private String canWebBuy; // 是否可预定 Y/N
  private String yp_info;
  private String train_seat_feature;
  private String location_code;
  private String is_support_card;
  private String controlled_train_flag;
  private String yp_ex;
  private String seat_types;
  private String exchange_train_flag;

  public static TicketDto build(String ticketInfoStr){
    TicketDto dto = new TicketDto();
    if(ticketInfoStr == null){ return dto; }

    String[] colmns = ticketInfoStr.split("\\|");
    if(colmns == null || colmns.length == 0){ return dto; }

    dto.setSecretHBStr(colmns[36]);
    dto.setSecretStr(colmns[0]);
    dto.setTrain_no(colmns[2]);
    dto.setStation_train_code(colmns[3]);
    dto.setStart_station_telecode(colmns[4]);
    dto.setEnd_station_telecode(colmns[5]);
    dto.setFrom_station_telecode(colmns[6]);
    dto.setTo_station_telecode(colmns[7]);
    dto.setStart_time(colmns[8]);
    dto.setArrive_time(colmns[9]);
    dto.setLishi(colmns[10]);
    dto.setCanWebBuy(colmns[11]);
    dto.setYp_info(colmns[12]);
    dto.setStart_train_date(colmns[13]);
    dto.setTrain_seat_feature(colmns[14]);
    dto.setLocation_code(colmns[15]);
    dto.setFrom_station_no(colmns[16]);
    dto.setTo_station_no(colmns[17]);
    dto.setIs_support_card(colmns[18]);
    dto.setControlled_train_flag(colmns[19]);
    dto.setGg_num(colmns[20] != null ? colmns[20] : "--");
    dto.setGr_num(colmns[21] != null ? colmns[21] : "--");
    dto.setQt_num(colmns[22] != null ? colmns[22] : "--");
    dto.setRw_num(colmns[23] != null ? colmns[23] : "--");
    dto.setRz_num(colmns[24] != null ? colmns[24] : "--");
    dto.setTz_num(colmns[25] != null ? colmns[25] : "--");
    dto.setWz_num(colmns[26] != null ? colmns[26] : "--");
    dto.setYb_num(colmns[27] != null ? colmns[27] : "--");
    dto.setYw_num(colmns[28] != null ? colmns[28] : "--");
    dto.setYz_num(colmns[29] != null ? colmns[29] : "--");
    dto.setZe_num(colmns[30] != null ? colmns[30] : "--");
    dto.setZy_num(colmns[31] != null ? colmns[31] : "--");
    dto.setSwz_num(colmns[32] != null ? colmns[32] : "--");
    dto.setSrrb_num(colmns[33] != null ? colmns[33] : "--");
    dto.setYp_ex(colmns[34]);
    dto.setSeat_types(colmns[35]);
    dto.setExchange_train_flag(colmns[36]);
//    dto.setFrom_station_name(cv[colmns[6]]);
//    dto.setTo_station_name(cv[colmns[7]]);
    return dto;
  }

  public String getSecretHBStr() {
    return secretHBStr;
  }

  public void setSecretHBStr(String secretHBStr) {
    this.secretHBStr = secretHBStr;
  }

  public String getSecretStr() {
    return secretStr;
  }

  public void setSecretStr(String secretStr) {
    this.secretStr = secretStr;
  }

  public String getTrain_no() {
    return train_no;
  }

  public void setTrain_no(String train_no) {
    this.train_no = train_no;
  }

  public String getStation_train_code() {
    return station_train_code;
  }

  public void setStation_train_code(String station_train_code) {
    this.station_train_code = station_train_code;
  }

  public String getStart_station_telecode() {
    return start_station_telecode;
  }

  public void setStart_station_telecode(String start_station_telecode) {
    this.start_station_telecode = start_station_telecode;
  }

  public String getEnd_station_telecode() {
    return end_station_telecode;
  }

  public void setEnd_station_telecode(String end_station_telecode) {
    this.end_station_telecode = end_station_telecode;
  }

  public String getFrom_station_name() {
    return from_station_name;
  }

  public void setFrom_station_name(String from_station_name) {
    this.from_station_name = from_station_name;
  }

  public String getTo_station_name() {
    return to_station_name;
  }

  public void setTo_station_name(String to_station_name) {
    this.to_station_name = to_station_name;
  }

  public String getFrom_station_no() {
    return from_station_no;
  }

  public void setFrom_station_no(String from_station_no) {
    this.from_station_no = from_station_no;
  }

  public String getTo_station_no() {
    return to_station_no;
  }

  public void setTo_station_no(String to_station_no) {
    this.to_station_no = to_station_no;
  }

  public String getFrom_station_telecode() {
    return from_station_telecode;
  }

  public void setFrom_station_telecode(String from_station_telecode) {
    this.from_station_telecode = from_station_telecode;
  }

  public String getTo_station_telecode() {
    return to_station_telecode;
  }

  public void setTo_station_telecode(String to_station_telecode) {
    this.to_station_telecode = to_station_telecode;
  }

  public String getStart_train_date() {
    return start_train_date;
  }

  public void setStart_train_date(String start_train_date) {
    this.start_train_date = start_train_date;
  }

  public String getStart_time() {
    return start_time;
  }

  public void setStart_time(String start_time) {
    this.start_time = start_time;
  }

  public String getArrive_time() {
    return arrive_time;
  }

  public void setArrive_time(String arrive_time) {
    this.arrive_time = arrive_time;
  }

  public String getLishi() {
    return lishi;
  }

  public void setLishi(String lishi) {
    this.lishi = lishi;
  }

  public String getGg_num() {
    return gg_num;
  }

  public void setGg_num(String gg_num) {
    this.gg_num = gg_num;
  }

  public String getGr_num() {
    return gr_num;
  }

  public void setGr_num(String gr_num) {
    this.gr_num = gr_num;
  }

  public String getQt_num() {
    return qt_num;
  }

  public void setQt_num(String qt_num) {
    this.qt_num = qt_num;
  }

  public String getRw_num() {
    return rw_num;
  }

  public void setRw_num(String rw_num) {
    this.rw_num = rw_num;
  }

  public String getRz_num() {
    return rz_num;
  }

  public void setRz_num(String rz_num) {
    this.rz_num = rz_num;
  }

  public String getTz_num() {
    return tz_num;
  }

  public void setTz_num(String tz_num) {
    this.tz_num = tz_num;
  }

  public String getWz_num() {
    return wz_num;
  }

  public void setWz_num(String wz_num) {
    this.wz_num = wz_num;
  }

  public String getYb_num() {
    return yb_num;
  }

  public void setYb_num(String yb_num) {
    this.yb_num = yb_num;
  }

  public String getYw_num() {
    return yw_num;
  }

  public void setYw_num(String yw_num) {
    this.yw_num = yw_num;
  }

  public String getYz_num() {
    return yz_num;
  }

  public void setYz_num(String yz_num) {
    this.yz_num = yz_num;
  }

  public String getZe_num() {
    return ze_num;
  }

  public void setZe_num(String ze_num) {
    this.ze_num = ze_num;
  }

  public String getZy_num() {
    return zy_num;
  }

  public void setZy_num(String zy_num) {
    this.zy_num = zy_num;
  }

  public String getSwz_num() {
    return swz_num;
  }

  public void setSwz_num(String swz_num) {
    this.swz_num = swz_num;
  }

  public String getSrrb_num() {
    return srrb_num;
  }

  public void setSrrb_num(String srrb_num) {
    this.srrb_num = srrb_num;
  }

  public String getCanWebBuy() {
    return canWebBuy;
  }

  public void setCanWebBuy(String canWebBuy) {
    this.canWebBuy = canWebBuy;
  }

  public String getYp_info() {
    return yp_info;
  }

  public void setYp_info(String yp_info) {
    this.yp_info = yp_info;
  }

  public String getTrain_seat_feature() {
    return train_seat_feature;
  }

  public void setTrain_seat_feature(String train_seat_feature) {
    this.train_seat_feature = train_seat_feature;
  }

  public String getLocation_code() {
    return location_code;
  }

  public void setLocation_code(String location_code) {
    this.location_code = location_code;
  }

  public String getIs_support_card() {
    return is_support_card;
  }

  public void setIs_support_card(String is_support_card) {
    this.is_support_card = is_support_card;
  }

  public String getControlled_train_flag() {
    return controlled_train_flag;
  }

  public void setControlled_train_flag(String controlled_train_flag) {
    this.controlled_train_flag = controlled_train_flag;
  }

  public String getYp_ex() {
    return yp_ex;
  }

  public void setYp_ex(String yp_ex) {
    this.yp_ex = yp_ex;
  }

  public String getSeat_types() {
    return seat_types;
  }

  public void setSeat_types(String seat_types) {
    this.seat_types = seat_types;
  }

  public String getExchange_train_flag() {
    return exchange_train_flag;
  }

  public void setExchange_train_flag(String exchange_train_flag) {
    this.exchange_train_flag = exchange_train_flag;
  }

  /**
   * 当前车次可选座位类型
   *
   * @see ticket.enums.SeatType
   * @return
   */
  public int getAvailableSeat() {
    int seatInfo = 0;
    if(available(this.swz_num)){
      seatInfo = seatInfo | SeatType.SW.getCode();
    }
    if(available(this.zy_num)){
      seatInfo = seatInfo | SeatType.YD.getCode();
    }
    if(available(this.ze_num)){
      seatInfo = seatInfo | SeatType.ED.getCode();
    }
    if(available(this.gr_num)){
      seatInfo = seatInfo | SeatType.GR.getCode();
    }
    if(available(this.rw_num)){
      seatInfo = seatInfo | SeatType.RW.getCode();
    }
    if(available(this.srrb_num)){
      seatInfo = seatInfo | SeatType.DW.getCode();
    }
    if(available(this.yw_num)){
      seatInfo = seatInfo | SeatType.YW.getCode();
    }
    if(available(this.rz_num)){
      seatInfo = seatInfo | SeatType.RZ.getCode();
    }
    if(available(this.yz_num)){
      seatInfo = seatInfo | SeatType.YZ.getCode();
    }
    if(available(this.wz_num)){
      seatInfo = seatInfo | SeatType.WZ.getCode();
    }
    if(available(this.qt_num)){
      seatInfo = seatInfo | SeatType.OTHER.getCode();
    }

    return seatInfo;
  }

  private boolean available(String seat){
    if(seat != null && (NumberUtils.isNumber(seat) || "有".equals(seat))){
      return true;
    }

    return false;
  }
}
